var utils = require('../../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checkbox1: ['配色不好看', '排版混乱', '界面简单', '界面丑陋'],
    checkbox2: ['评论功能异常', '消息发布异常','个人主页异常','消息提醒异常','功能欠缺', '加载卡顿'],
    checkbox3: ['垃圾信息多','社区不活跃','目标无法实现'],
    pageID: '',
    type: '',
    imgnum:0,
    figureUrl:[],
    figureID:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var user = wx.getStorageSync('user')
    if (user[options.id] == [options.creator, options.tourist]) {
      wx.showModal({
        title: '提示',
        content: '你已经举报过该条内容了，无需重复举报',
        showCancel: false,
        success: res => {
          // 使用navigateBack就不会导致openid的流失，不必重新加载页面
          wx.navigateBack({
            url: '/pages/details/details?id=' + options.id + '&tourist=' + options.tourist,
          })
        }
      })
    }
    this.data.pageID = options.id
    this.data.type = options.type
    this.data.tourist = options.tourist
    this.data.creator = options.creator
    this.data.content = options.content
  },

  addimages: function (e) {
    var openid = wx.getStorageSync('openID')
    var that = this
    var img = new Array()
    var time = utils.formatTime(new Date())
    wx.chooseImage({
      count: 3,
      success: function (res) {
        wx.showToast({
          title: '正努力加载图片',
          duration: 2000,
          mask: true,
          image: 'shenglvehao.png'
        })
        var tempFilePaths = res.tempFilePaths
        if (that.data.imgnum + tempFilePaths.length < 4) {
          that.data.figureUrl = that.data.figureUrl.concat(tempFilePaths)
          // 用于计数
          var figurenum = new Array()
          figurenum = figurenum.concat(tempFilePaths)
          that.data.imgnum += figurenum.length
          that.setData({ figures: that.data.figureUrl })
          for (var num = -1; num < that.data.figureUrl.length;) {
            num++
            var name = time.substring(0, 5) + '/' + openid + '_' + num + '_' + Math.floor(Math.random() * 100)
            wx.cloud.uploadFile({
              cloudPath: name+'.png',
              filePath: that.data.figureUrl[num],
              success: res => {
                that.data.figureID.push(res.fileID)
              },
              fail:res => {
                console.error
              }
            })
          }
        }
        else {
          wx.showModal({
            title: '提示',
            content: '上传的图片数量不能超过3张哦',
          })
        }
      }
    })
  },

  delimages: function (e) {
    // 增加调用deleteFile API节省存储空间
    wx.cloud.deleteFile({
      fileList: this.data.figureID
    })
    this.data.figureID = []
    this.data.imgnum = 0
    this.data.figureUrl = []
    this.setData({ figureID: [], figures: [] })
  },

  submitForm(e) {
    var type = '用户反馈'
    var openid = wx.getStorageSync('openID')
    
    wx.showLoading({
      title: '正在发送',
    })
    if (e.detail.value.ui != "" || e.detail.value.functions != "" || e.detail.value.community != "" || e.detail.value.other != "") {
      wx.cloud.callFunction({
        name: 'sendEmail',
        data: {
          content: e.detail.value,
          openid: openid,
          figureID: this.data.figureID,
          operation:'suggestion'
        },
        success: res => {
          wx.showToast({
            title: '感谢您的反馈',
          })
          wx.hideLoading()
          wx.reLaunch({
            url: '/pages/me/me'
          })
        }
      })
    }
    else {
      wx.showToast({
        title: '请选择反馈原因',
        image: '/images/close-circle.png'
      })
    }
  }
})